﻿using System;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;

using LTJ.Entity;
using LTJ.Common;
using LTJ.Common.Generic;
using LTJ.Config;
using LTJ.FrameWork;

namespace LTJ.WebPage.UserManage.rule
{
    /// <summary>
    /// 规则编辑
    /// </summary>
    public partial class ruleedit : UserPage
    {
        protected string opmode = SASRequest.GetString("opmode");
        protected int rtype = SASRequest.GetInt("rtype", 0);
        protected int rid = SASRequest.GetInt("rid", 0);
        protected RuleInfo rinfo = new RuleInfo();
        protected int gtype = 1;

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void editrule_Click(object sender, EventArgs e)
        {
            int ruletype = gtype;

            rinfo.uid = SASRequest.GetInt("ajaxcorpuserdatalist_value", 0);
            rinfo.oid = SASRequest.GetInt("ajaxcorpdatalist_value", 0);
            rinfo.rid = SASRequest.GetInt("ajaxcorproutedatalist_value", 0);
            rinfo._20GP = price1.Text;
            rinfo._40GP = price2.Text;
            rinfo._40HQ = price3.Text;
            rinfo.port = SASRequest.GetString("ajaxcorpportdatalist_value");
            rinfo.rtype = SASRequest.GetInt("endtype", 0);

            if (opmode == "add") ruletype = SASRequest.GetInt("ruletype", 0);

            if (ruletype > 1)
            {
                rinfo.uid = 0;
                rinfo.oid = 0;
                rinfo.rtype = 0;

                if (ruletype == 2)
                {
                    if (rtype == 1) rinfo.uid = -2;
                    if (rtype == 2) rinfo.oid = -2;
                }
                if (ruletype == 3)
                {
                    if (rtype == 1) rinfo.uid = -2;
                    if (rtype == 2) rinfo.uid = -3;
                    rinfo.rid = 0;
                    rinfo.oid = companyinfo.id;
                }

                rinfo.port = "";
                
            }

            if (ruletype == 1)
            {
                if (rtype == 1 && rinfo.uid == 0)
                {
                    base.RegisterStartupScript("PAGETEMPLATE", "请选择用户！");
                    return;
                }
                else if (rtype == 2 && rinfo.oid == 0)
                {
                    base.RegisterStartupScript("PAGETEMPLATE", "请选择企业！");
                    return;
                }
            }
            if (ruletype < 3)
            {
                if (rinfo.rid == 0)
                {
                    base.RegisterStartupScript("PAGETEMPLATE", "请选择授权航线！");
                    return;
                }
                else if (rinfo.rtype == 2 && rinfo.port == "" && ruletype == 1)
                {
                    base.RegisterStartupScript("PAGETEMPLATE", "港口类型的规则，请选择相应港口！");
                    return;
                }
            }

            if (opmode == "add")
            {
                rinfo.id = Rules.CreateRules(rinfo.uid, rinfo.oid, rinfo.rtype, rinfo._20GP, rinfo._40GP, rinfo._40HQ, rinfo.rid, rinfo.port, rinfo.seacode);
                if (rinfo.id == -1)
                {
                    base.RegisterStartupScript("PAGETEMPLATE", "该航线规则已经存在，请在规则列表中查找进行修改！");
                    return;
                }
                OpLogs.LogRuleAdd(userid, username, companyinfo.id, rinfo.id, rinfo._20GP, rinfo._40GP, rinfo._40HQ);
            }
            else if (opmode == "edit")
            {
                Rules.UpdateRulePrice(rinfo.id, price1.Text, price2.Text, price3.Text);
                OpLogs.LogRuleEdit(userid, username, companyinfo.id, rinfo.id, rinfo._20GP, rinfo._40GP, rinfo._40HQ, "");
            }

            base.RegisterStartupScript("PAGE", "parent.document.getElementById('reclick').click();parent.Lightview.hide();");
        }

        #region Web 窗体设计器生成的代码

        override protected void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }

        private void InitializeComponent()
        {
            if (opmode == "edit")
            {
                rinfo = Rules.GetRuleInfoByID(rid);
                price1.Text = rinfo._20GP;
                price2.Text = rinfo._40GP;
                price3.Text = rinfo._40HQ;

                if (rinfo.rtype == 0)
                {
                    if (rinfo.rid > 0) gtype = 2;
                    else gtype = 3;
                }
                else gtype = 1;
            }

            editrule.Click += new EventHandler(editrule_Click);
        }

        #endregion
    }
}